extends Control
class_name TipBook

@onready var v_box_container: VBoxContainer = $MarginContainer/ScrollContainer/VBoxContainer


func _ready() -> void:
	Global.tip_book = self
	GameEvents.stage_entered.connect(on_stage_entered)
	GameEvents.unlock_tip.connect(func(index): v_box_container.get_child(index).visible = true)

func enter() -> void:
	visible = true
	MusicPlayer.stop_all_sfx()
	get_tree().paused = true
	var tween = create_tween()
	tween.set_parallel()
	tween.set_ignore_time_scale()
	tween.tween_property(self,"modulate:a",1,0.5).from(0)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)

func on_stage_entered() -> void:
	for i in range(2,v_box_container.get_child_count()):
		v_box_container.get_child(i).visible = false
	match Global.current_stage_name:
		"stage1":
			for i in range(2,5):
				v_box_container.get_child(i).visible = true
		"stage2":
			for i in range(2,8):
				v_box_container.get_child(i).visible = true
		"stage3":
			for i in range(2,9):
				v_box_container.get_child(i).visible = true
		"stage4":
			for i in range(2,14):
				v_box_container.get_child(i).visible = true
		"stage5":
			v_box_container.get_child(15).visible = true
	if Global.current_setting.get("MODE",2)==0:
		v_box_container.get_child(16).visible = true

func exit() -> void:
	var tween = create_tween()
	tween.set_parallel()
	tween.set_ignore_time_scale()
	tween.tween_property(self,"modulate:a",0,0.5).from(1)\
	.set_ease(Tween.EASE_IN_OUT).set_trans(Tween.TRANS_CUBIC)
	tween.chain().tween_callback(func(): 
		visible = false
		get_tree().paused = false
		)

func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("tip"):
		if visible == false:
			enter()
		else:
			exit()
	
